import request from '/@/utils/http/axios';
import { useUserStore } from '/@/store';
import type { Homework } from '/@/entity/Homework';
import {get} from '/@/utils/http/axios';

const userStore = useUserStore();

// 发布作业
export const publishHomeworkApi = async (data: {
  courseId: number;
  title: string;
  description: string;
  deadline: string;
  file?: File;
}) => {
  const formData = new FormData();
  // 独立添加字段，而非JSON字符串
  formData.append('courseId', data.courseId.toString());
  formData.append('title', data.title);
  formData.append('description', data.description);
  formData.append('deadline', data.deadline);
  if (data.file) {
    formData.append('file', data.file);
  }

  return request({
    method: 'post',
    url: '/api/hwork/publish',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data',
      'user-id': userStore.user_id.toString()
    }
  });
};

// 获取教师已发布作业
export const getPublishedHomeworkApi = async (): Promise<Homework[]> => {
  const userStore = useUserStore();
  const response = await request<{ data: Homework[] }>({
    method: 'get',
    url: '/api/hwork/published',
    headers: {
      'user-id': userStore.user_id
    }
  });
  return response.data;
};

// 批改作业
export const correctHomeworkApi = async (id: number, score: number) => {
  return request({
    method: 'post',
    url: `/api/hwork/correct/${id}`,
    params: { score }
  });
};

// 获取作业详情
export const getHomeworkDetailApi = async (id: number): Promise<Homework> => {
  const userStore = useUserStore(); // 获取用户存储
  const response = await request<{ data: Homework }>({
    method: 'get',
    url: `/api/hwork/${id}`,
    headers: {
      'user-id': userStore.user_id.toString() // 添加请求头
    }
  });
  return response.data;
};

// 获取学生作业
export const getStudentHomeworkApi = async (
  studentId: number, 
  status: 0 | 1 | 2
): Promise<Homework[]> => {
  const response = await request<{ data: Homework[] }>({
    method: 'get',
    url: '/api/hwork/student',
    params: { studentId, status }
  });
  return response.data;
};

// 提交作业
export const submitHomeworkApi = async (data: {
  homeworkId: number;
  answer: string;
  file?: File;
}) => {
  const formData = new FormData();
  formData.append('answer', data.answer);
  if (data.file) {
    formData.append('file', data.file);
  }

  return request({
    method: 'post',
    url: `/api/hwork/submit/${data.homeworkId}`,
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
};

export const getTeachingStudents = async (teacherUserId: string): Promise<number[]> => {
  const response = await request<{ data: number[] }>({
    method: 'get',
    url: '/api/hwork/analysis/students',
    params: { teacherUserId }
  });
  return response.data;
};

// 获取教学分析数据
export const getTeachingAnalysisData = async (
  teacherUserId: string,
  studentId?: number
): Promise<Record<string, Array<{ score: number; time: string }>>> => {
  const response = await request<{ data: Record<string, Array<{ score: number; time: string }>> }>({
    method: 'get',
    url: '/api/hwork/analysis/scores',
    params: { 
      teacherUserId,
      studentId: studentId || undefined
    }
  });
  return response.data;
};

// 获取教师所教学生列表
// export const getTeachingStudents = (teacherUserId: string) => {
//   return get<{ data: number[] }>({
//     url: '/hwork/analysis/students',
//     params: { teacherUserId }
//   }).then(res => res.data);
// };

// export const getTeachingAnalysisData = (teacherUserId: string, studentId?: number) => {
//   return get<{ data: Record<string, Array<{ score: number; time: string }>> }>({
//     url: '/hwork/analysis/scores',
//     params: { teacherUserId, studentId }
//   }).then(res => res.data);
// };